package com.example.faqdemo.controller;

import com.example.faqdemo.entity.Feedback;
import com.example.faqdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserService userService;

    @PostMapping
    public Feedback submitFeedback(@RequestBody Feedback feedback, @RequestParam Integer userId) {
        User user = userService.findByUsername(userId.toString());
        if (user != null) {
            feedback.setUserId(user.getId());
            return feedbackService.saveFeedback(feedback);
        }
        return null;
    }

    @GetMapping
    public List<Feedback> getAllFeedbacks(@RequestParam Integer userId) {
        User user = userService.findByUsername(userId.toString());
        if (user != null && user.getType() == 1) {
            return feedbackService.getAllFeedbacks();
        }
        return null;
    }
}
